<?php
namespace app\logistics\Service;

use think\Service;
use app\logistics\model\LogisticsRules as LogisticsRulesModel;

/**
 * 物流规则
 * @author huangfeng
 * @date 2024-7-2
 * @version 1.0
*/
class LogisticsRules extends Service
{
    protected $LogisticsRulesModel;

    /**
     * 构造方法
     * @param void
     * @return void
     * @author huangfeng
     * @date 2024-7-2
     */
    public function __construct()
    {
        $this->LogisticsRulesModel = new LogisticsRulesModel();
    }

    /**
     * 获取条件物流规则
     * @param array $condition 筛选条件
     * @param int $page 当前页码
     * @param int $limit 每页显示数量
     * @return array
     */
    public function getlist($condition = [])
    {
        $where = [];
        if (isset($condition['rule_name']) && $condition['rule_name']) {
            $where['rule_name'] = ['like', '%' . $condition['rule_name'] . '%'];
        }
        if (isset($condition['logistics_name']) && $condition['logistics_name']) {
            $where['logistics_name'] = ['=', $condition['logistics_name']];
        }
        if (isset($condition['terms']) && $condition['terms']) {
            $where['terms'] = ['=', $condition['terms']];
        }
        if (isset($condition['from_nation']) && $condition['from_nation']) {
            $where['from_nation'] = ['=', $condition['from_nation']];
        }
        if (isset($condition['to_nation']) && $condition['to_nation']) {
            $where['to_nation'] = ['=', $condition['to_nation']];
        }
        if (isset($condition['is_active']) && $condition['is_active']) {
            $where['is_active'] = ['=', $condition['is_active']];
        }
        $where['is_del'] = 0;
        $arrData = ['code' => 0, 'message' => 'success'];
        $strField = ['id','rule_name','logistics_name','terms','freight_tag','from_nation','to_nation','model','weight_lower','weight_upper','transfer_fee','single_fee','magnetic_inspection_fee','customs_declaration_fee','insurance_rate','insurance_fee','freight','level','is_active','creator','create_time','update_time','updater'];
        $total =  $this->LogisticsRulesModel->where($where)->count();

        $list = $this->LogisticsRulesModel->where($where)->field($strField)->append(['logistics_name_text','from_nation_text','to_nation_text'])->page($condition['page'],$condition['pagesize'])->select();
        $arrData['data'] = ['list'=>$list->toArray(),'total'=>$total];
        return $arrData;
    }

    /**
     * 获取激活的物流规则
     * @param array $data['logistics_name','terms']  物流公司和贸易规则
     * @return array
     */
    public function getActiveList($data)
    {
        $where = [];
        if (isset($data['logistics_name']) && $data['logistics_name']) {
            $where['logistics_name'] = ['=', $data['logistics_name']];
        }
        if (isset($data['terms']) && $data['terms']) {
            $where['terms'] = ['=', $data['terms']];
        }
        $where['is_del'] = 0;
        $where['is_active'] = 1;
        $arrData = ['code' => 0, 'message' => 'success'];
        $strField = ['id','rule_name','freight_tag','from_nation','to_nation','model','weight_lower','weight_upper','transfer_fee','single_fee','magnetic_inspection_fee','customs_declaration_fee','insurance_rate','insurance_fee','level','freight'];
        $list = $this->LogisticsRulesModel->where($where)->field($strField)->order('level ASC,update_time DESC')->select();
        $arrData['data'] = $list->toArray();
        return $arrData;
    }
    
    /**
     * 添加物流规则
     * @param array $data 物流规则数据
     * @return array
     */
    public function add($data)
    {
        $data['create_time'] = date("Y-m-d H:i:s", time());
        $data['is_del'] = 0;
        $rules = $this->LogisticsRulesModel->save($data);
        if ($rules){
            return ['code' => 0, 'message' => '新增物流规则成功！'];
        }else{
            return ['code' => 1, 'message' => '新增物流规则失败！'];
        }
    }

    /**
     * 更新物流规则
     * @param int $id 物流规则ID
     * @param array $data 更新的物流规则数据
     * @return array
     */
    public function update($data)
    {
        $where = [['is_del','=','0'],['id','=', $data['id']]];
        $rules = $this->LogisticsRulesModel->where($where)->find();
        if (!$rules) {
            return ['code' => 1, 'message' => '获取物流规则失败！'];
        }
        $data['update_time'] = date("Y-m-d H:i:s", time());
        if ($rules->save($data)){
            return ['code' => 0, 'message' => '更新物流规则成功！'];
        }else{
            return ['code' => 1, 'message' => '更新物流规则失败！'];
        }
    }

     /**
     * 删除物流规则
     * @param int $id 物流规则ID
     * @return array
     */
    public function delete($id)
    {
        $where = [['is_del','=','0'],['id','=', $id]];
        $rules = $this->LogisticsRulesModel->where($where)->find();
        if (!$rules) {
            return ['code' => 1, 'message' => '获取物流规则信息失败！'];
        }
        if ($rules->save(['is_del' => time()])){
            return ['code' => 0, 'message' => '删除物流规则成功！'];
        }else{
            return ['code' => 1, 'message' => '删除物流规则失败！'];
        }
    }

    /**
     * 获取物流规则信息
     * @param int $id 物流规则ID
     * @return array
     */
    public function getInfo($id)
    {
        $where = [['is_del','=','0'],['id','=', $id]];
        $strField = ['id','rule_name','logistics_name','terms','freight_tag','from_nation','to_nation','model','weight_lower','weight_upper','transfer_fee','single_fee','magnetic_inspection_fee','customs_declaration_fee','insurance_rate','insurance_fee','level','is_active','freight','creator','create_time','update_time','updater','notes'];
        $list = $this->LogisticsRulesModel->where($where)->field($strField)->append(['logistics_name_text','from_nation_text','to_nation_text'])->find();
        $arrData = ['code' => 0, 'message' => 'success', 'data' => []];
        if (!$list) {
            $arrData['code'] = 1;
            $arrData['message'] = '获取物流规则信息失败！';
        }else{
            $arrData['data'] = $list->toArray();
        }
        return $arrData;
    }
}
